package com.example.liyun.joy;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.liyun.joy.serviceutils.JokeBean;
import com.example.liyun.joy.serviceutils.LoaderResponce;
import com.example.liyun.joy.serviceutils.NewsBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private BottomNavigationView mBottomnavigationView;
    public MenuItem menuItem;//与viewpager关联的item，在BottomNavigationListener中初始化
    private ViewPagerAdapter viewPagerAdapter;
    private List<Fragment> mList=new ArrayList<Fragment>();//设置Fragment的集合

    private DrawerLayout drawerLayout;


    public static Context mContext;

    /**
     * 获得一个全局Context
     * @return
     */
    public static Context getmContext() {
        return mContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=findViewById(R.id.toolbar);
        super.setSupportActionBar(toolbar);
        //手机顶部状态栏与toolbar颜色一致
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            WindowManager.LayoutParams barLayoutParams=getWindow().getAttributes();
            barLayoutParams.flags=(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS|barLayoutParams.flags);
        }
        mContext=getApplicationContext();

        /*toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点中了toolbar左侧的侧拉按钮事件",Toast.LENGTH_LONG).show();
            }
        });*/

        //抽屉DrawerLayout
        drawerLayout=findViewById(R.id.drawer_layout);
        //与DrawerLayout，toolbar关联，监听侧滑
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,
                drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();//关联配置

        NavigationView navigationView=findViewById(R.id.nav_view);
        //为菜单内的item添加监听事件
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.nav_camera:
                        break;
                    case R.id.nav_gallery:
                        break;
                    case R.id.nav_slideshow:
                        break;
                    case R.id.nav_manage:
                        break;
                    case R.id.nav_share:
                        break;
                    case R.id.nav_send:
                        break;
                }
                //选择某个Item后，关闭侧滑菜单
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });


        //设置底部导航栏
        initBottomnavigationView();

        //test();

    }
    /*final List<JokeBean.ResultBean.DataBean> jokeDatas = new ArrayList<>();
    private void test() {

        LoaderResponce mLoader=new LoaderResponce();
            mLoader.getDatas(1,20).subscribe(new Consumer<List<JokeBean.ResultBean.DataBean>>() {
                @Override
                public void accept(final List<JokeBean.ResultBean.DataBean> dataBeans) throws Exception {

                    jokeDatas.addAll(dataBeans);
                    Log.d("dataBeans","joke111:"+jokeDatas.size());


                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    Log.d("dataBeans", "trowable" + throwable.toString());
                }
            });

        Log.d("dataBeans","joke:"+jokeDatas.size());

        final List<NewsBean.ResultBean.DataBean> newsDatas=new ArrayList<>();
            mLoader.getDatas("top").subscribe(new Consumer<List<NewsBean.ResultBean.DataBean>>() {
                @Override
                public void accept(List<NewsBean.ResultBean.DataBean> dataBeans) throws Exception {
                    Log.d("dataBeans","news"+dataBeans.get(1).getTitle());
                    //newsDatas.addAll(dataBeans);
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    Log.d("dataBeans","test");
                    Log.d("dataBeans","trowable"+throwable.toString());
                }
            });


    }*/

    /**
     * 设置底部导航栏
     */
    private void initBottomnavigationView() {
        mViewPager=findViewById(R.id.vp_fragment);
        mBottomnavigationView=findViewById(R.id.bn_view);

        //设置底部导航栏Item监听事件
        BottomNavigationListener mbottomNavigationListener=new BottomNavigationListener(mViewPager);
        mBottomnavigationView.setOnNavigationItemSelectedListener(mbottomNavigationListener);
        //使底部导航栏的Item均分宽度(一般超过3个Item不会自动均分宽度)   BottomNavigationViewHelper.disableShiftMode

        //得到底部导航栏点击的是哪个Item
        menuItem=mbottomNavigationListener.getMenuItem();

        //设置View Pager的监听事件,这里的set换成了add。注意
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             *
             * @param i
             * @param v
             * @param i1
             */
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (menuItem!=null){
                    menuItem.setChecked(false);
                }else {//默认第一个Item选中
                    mBottomnavigationView.getMenu().getItem(0).setChecked(false);
                }
                menuItem=mBottomnavigationView.getMenu().getItem(i);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //设置Viewpager的适配器
        viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(viewPagerAdapter);
        //将Fragment添加进List集合里
        Fragment fragment1=new ViewPagerFragment("资讯Fragment");
        mList.add(fragment1);
        Fragment fragment2=new ViewPagerFragment("段子Fragment");
        mList.add(fragment2);
        //为ViewPager设置Fragment
        viewPagerAdapter.setList(mList);


    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {//如果开启，则关闭
            drawerLayout.closeDrawer(GravityCompat.START);
        }else {
            super.onBackPressed();
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.search_view:
                Intent searchIntent=new Intent(MainActivity.this,SearchActivity.class);
                startActivity(searchIntent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        /*SearchView mSearchView= (SearchView) menu.findItem(R.id.search_view).getActionView();
        mSearchView.setQueryHint("搜索");*/
        return true;
    }
}
